<template>
    <view>
        <custom><template #backText>确认订单</template></custom>
        <view class="bg-gradual-tb-nddyny padding-lr-24 padding-top-20 padding-bottom-70">
            <view class="cu-list menu radius-16 bg-white">
                <template v-if="data.form.buy_mode != '1'">
                    <nMenuItemRow1 v-if="!address.address_id" left_icon="cuIcon-locationfill" uri="/user/address/list?is_select=1"
                        title="请选择收货地址" right_icon="arrow" class_title="text-15 padding-tb-30"></nMenuItemRow1>
                    <nMenuItemRow2 v-else left_icon="cuIcon-locationfill" :top_title="address.province_city_area_name + ' ' + address.address_remark"
                        :bottom_title="address.name + (address.sex==0?'(先生)':(address.sex==1?'(女士)':'')) + ' ' + address.phone"
                        uri="/user/address/list?is_select=1" right_icon="arrow" class_top_title="text-15"></nMenuItemRow2>
                </template>
                <nMenuItemRow1 title="支付方式" right_icon="arrow" arrow_title="支付宝"></nMenuItemRow1>
                <nFormItem type="switch" title="消费余额" :title_number="computedTitle2AdvpayAmount" :data.sync="data"
                    field="use_advpay_amount" format="number">
                    <template #switchLeft>
                        <view v-if="data.price_info.pay_advpay_amount > 0" class="margin-right-10 text-red">-{{data.price_info.pay_advpay_amount}}</view>
                    </template>
                </nFormItem>
                <nFormItem type="switch" title="消费红包" :title_number="computedTitle2VoucherAmount" :data.sync="data"
                    field="use_voucher_amount" format="number">
                    <template #switchLeft>
                        <view v-if="data.price_info.pay_voucher_amount > 0" class="margin-right-10 text-red">-{{data.price_info.pay_voucher_amount}}</view>
                    </template>
                </nFormItem>
            </view>
        </view>
        <cart style="margin-top: -70rpx;" ref="cart" page_type="pay" :paySubmit="paySubmit">
            <template #top="_data">
                <view class="padding-lr-24 padding-top-20">
                    <view class="cu-list menu radius-16 bg-white">
                        <nMenuItemRow1 v-if="data.form.buy_mode!='1'" :title="seller_buy_mode_int2string[data.form.buy_mode] + (data.form.buy_mode=='0' ? '配送费' : (data.form.buy_mode=='2' ? '运费' : ''))">
                            <template #right>
                                <text class="text-red text-bold">￥{{_data.pay.price_info.total_recv_price}}</text>
                            </template>
                        </nMenuItemRow1>
                        <nMenuItemRow1 v-else title="到店自取"></nMenuItemRow1>
                    </view>
                </view>
            </template>
            <template #foot>
                <view class="padding-lr-24 margin-bottom-30">
                    <nFormItem type="textarea" title="订单备注" :data.sync="pay" field="remark" radius="radius-16" value="最多100个字"
                        maxlength="100" row="3"></nFormItem>
                </view>
            </template>
        </cart>
        <foot v-if="!address.address_id && data.form.buy_mode != 1" title="请选择收货地址" uri="/user/address/list?is_select=1"></foot>
        <pay :pay.sync="pay" :paySuccess="paySuccess"></pay>
    </view>
</template>

<script>
    import cart from './components/cart.vue';
    export default {
        components: {
            cart,
        },
        data() {
            return {
                seller_buy_mode_int2string: this.$dict.seller_buy_mode_int2string,
                data: {
                    price_info: {
                        pay_advpay_amount: 0,
                        pay_voucher_amount: 0
                    },
                    form: {
                        page_type: 'pay',
                        seller_type: this.$store.state.user.is_seller_firm == 1 ? 1 : 0,
                        buy_mode: '',
                        items: '',
                        address_id: '',
                        use_advpay_amount: '0',
                        use_voucher_amount: '0'
                    }
                },
                address: {
                    address_id: '',
                    province_city_area_name: '',
                    address_remark: '',
                    name: '',
                    phone: '',
                    tag: '',
                    sex: '',
                    is_default: ''
                },
                pay: {
                    formtag: {},
                    form: {
                        remark: '',
                    },
                    tip_content: '确定支付吗？',
                    pay_uri: '/userPay/sellerItem/pay',
                    show: false,
                    load_info: {},
                },
            }
        },
        onLoad(params) {
            if (!params.buy_mode || !params.items) {
                this.$store.commit('appBackPage');
                return;
            }
            this.data.form.buy_mode = params.buy_mode;
            this.data.form.items = params.items;
            this.pay.load_info = params;
        },
        mounted() {
            this.$refs.cart.setBuyMode(this.data.form.buy_mode);
        },
        onShow() {
            this.pageInit();
        },
        watch: {
            'pay.formtag.address_id'() {
                if (this.pay.formtag.address_id) {
                    this.$store.commit('userToastNone', '请选择收货地址');
                    this.pay.formtag.address_id = '';
                }
            },
            'address.address_id'(new_value) {
                this.data.form.address_id = new_value;
            },
            'data.form.use_advpay_amount'() {
                this.pageInit();
            },
            'data.form.use_voucher_amount'() {
                this.pageInit();
            }
        },
        computed: {
            computedTitle2AdvpayAmount() {
                return this.computedTitle2Amount('advpay_amount');
            },
            computedTitle2VoucherAmount() {
                return this.computedTitle2Amount('voucher_amount');
            }
        },
        methods: {
            computedTitle2Amount(field) {
                let amount = this.$nddyny.number.floor(this.$store.state.user.user_asset_info[field]);
                let result = amount;
                if (this.data.price_info['pay_' + field] > 0) {
                    result = this.$nddyny.bc.sub(
                        result, this.data.price_info['pay_' + field]
                    );
                }
                return result;
            },
            pageInit(action) {
                this.$api.post(this, {
                    url: '/seller/itemCart/list',
                    success: (Result, data_key) => {
                        if (!this.$R.noSuccess(Result)) {
                            let price_info = Result.result.price_info;
                            let user_address_info = Result.result.user_address_info;
                            this.data.price_info.pay_advpay_amount = price_info.pay_advpay_amount;
                            this.data.price_info.pay_voucher_amount = price_info.pay_voucher_amount;
                            this.data.form.use_advpay_amount =
                                this.data.price_info.pay_advpay_amount > 0 ? '1' : '0';
                            this.data.form.use_voucher_amount =
                                this.data.price_info.pay_voucher_amount > 0 ? '1' : '0';
                            if (user_address_info) {
                                this.setAddress(user_address_info);
                            }
                        }
                        this.$nextTick(() => {
                            this.$refs.cart.setCartList(Result, data_key, 'payPageInit');
                        })
                    }
                });
            },
            backData(info) {
                this.setAddress(info);
            },
            setAddress(info) {
                this.address = info;
                this.pay.form.address_id = info.address_id;
            },
            paySuccess() {
                this.$store.commit('userLaunchTo', '/seller/item/order/list');
            },
            paySubmit(buy_mode, item_attr_ids, item_attr_list, cart_item_attr_list) {
                if (this.data.form.buy_mode != '1' && !this.address.address_id) {
                    this.$store.commit('userToastNone', '请选择收货地址');
                    return;
                }
                let pay_form = {
                    pay_type: 1,
                    seller_type: this.data.form.seller_type,
                    buy_mode: this.data.form.buy_mode,
                    items: this.data.form.items,
                    address_id: this.data.form.address_id,
                    use_advpay_amount: this.data.form.use_advpay_amount,
                    use_voucher_amount: this.data.form.use_voucher_amount,
                    remark: this.pay.form.remark
                };
                this.$set(this.pay, 'form', pay_form);
                this.pay.show = true;
            }
        }
    }
</script>
